Repository navigation
Put inline math on the text baseline on iOS - #314
Conversation
With a lineHeight, React Native's iOS text raises glyphs by half the line's extra height but places inline views from the line's bottom and the font's descender without that raise, so inline math sat that far below the text. That is the answered quiz card's math sitting low and, in messages, the period after inline math looking raised (#313). Each inline picture is now lifted by the same amount on iOS; Android places views on the baseline and gets no lift. Verified: checks
|
Preview ready for lh preview 314It runs beside your server on port 3775 with fresh, empty data, and Ctrl+C stops it and deletes it. Release files. Each push builds a new one. |
…aseline Measured on the iPhone check, inline math sat about 4.2 pt low on 16/24 lines and 3 pt low on 14/20 lines, far more than the baseline offset alone. React Native 0.88's iOS text puts an inline view's bottom at its line's bottom plus its character's font descender, and that character has no font, so TextKit's default Helvetica 12 is used. The lift now undoes exactly that: the text's baseline (its descent plus React Native's baseline offset, over every font in the line, at the font scale) less Helvetica 12's 2.76 pt descender. A Text with only math gets no lift, and on iOS a line grows to hold tall or deep math once lifted. A test fails when React Native changes this placement. Verified: checks
|
Round 1 review, addressed in 6a27bf4. Important
Nits
Also seen, separate from the baseline: in the pre-fix shots, a math-only signed pair |
|
Measured, before and after (iPhone check, 3x pictures). Before is run 44871251269 (main, no lift); after is run 44884978315 (6a27bf4, green). Each gap is the bottom of the math's ink (a digit, or
-1 px is one third of a point, which is anti-aliasing at the ink threshold. Dark matches light (the Also checked in the after run:
Cut tops, unchanged and not caused by this PR. In a Text that holds only math (the choice rows |
…lines TextKit sets each line's baseline from that line's fonts, so the lift now uses the descent of the hard line the math is on, and none for a hard line holding only math, as for a math-only label. React Native's baseline offset is worked out over the whole Text, so that part still uses every font. Instead of growing every line of a paragraph for tall math on iOS, the lift is capped so a picture never rises above its line's top; line heights are back to what they were. Inline code's size is one constant, the choice-row check uses the real 21 pt line, and the iPhone fixture gains a tall rule alone after a newline and a plain line above a line with a tall rule. Verified: checks
|
Round 2 review, addressed in 647eeb9. Each fix has a test that fails on 6a27bf4. Important (Codex)
Nits (Claude)
Proof: the iPhone fixture's final reply gains |
|
Measured on 647eeb9 (run 44889291506, green; 3x pictures). Gap = bottom of the math's ink minus bottom of the letters on the same line, in px (positive = math low). Pages shifted with the longer fixture, so each spot was found by matching pixel rows with round 1 (page 2 matches at +496 px with zero difference).
Line heights. Ordinary prose lines advance 72 px (24 pt): the "The first pair fits" paragraph's three lines. The text rows of every measured line are where they were in round 1. The choice rows are pixel-identical to round 1, so they're still 21 pt. The math-only choice pairs are unchanged (ink rows 1518-1557 and 1792-1831). The new tall-rule paragraphs (
Separate, not filed. The |
|
prove-learnharness: PROVED for 647eeb9 Couldn't prove physical TestFlight or VoiceOver speech from the CI artifacts. All checks requested for this diff passed.
Receipt Proof receiptReceipt for PR #314, iPhone inline math baselineVerdict: PROVED What the change touches
iPhone observationsThese are ink-bottom gaps in screenshot pixels. Positive means the math is lower than the neighboring letters. Light and dark are listed separately. Phone captures are 3x. CI web pairs are 1x and the supplemental web explanation is 3x. The measurement uses columns with no descending letters and masks at luminance 145 for light or 125 for dark. The web explanation uses a lighter threshold for its muted text. Anti-aliasing shifts some ink edges by one pixel.
Android and mechanical checks
Android region comparisons exclude system status chrome. See Probes
Retries and fixes
Not coveredPhysical TestFlight, VoiceOver speech and store-track acceptance are outside this diff's CI and emulator proof. There was no OTA publication. Known and filed observations: #315 math-only top clipping and #317 rule color. Left behindEvidence in the two proof folders and the isolated temporary checkout. The test APK was uninstalled, the default keyboard was restored to Gboard and read back, and the disposable server, TLS proxy and fake model were stopped. |
…#318) Fixes two phone math bugs. **#315, iPhone: a line of only inline math was clipped at the top and bottom.** React Native's Text clips to its frame. A Text with no text gets no line height, and Fabric places the picture 2.76 pt too high (a fontless attachment's Helvetica 12 descender). - **The fix:** math in a text-less Text moves down 2.76 pt plus a 0.5 pt pixel-rounding margin (3.26 pt in all). - **Padding:** the Text gets bottom padding for the math's depth plus 1 pt, added to any existing padding such as a table cell's. - **Growth:** math-only rows and lines grow by about 5 to 6 pt, and nothing else changes size. **#317: `\rule` drew black.** - **The fix:** a rule now takes the colour of its nearest coloured ancestor, as glyphs do, else the text colour. Only MathJax's literal `black` default is rewritten. - **Edge cases:** zero-size struts are ignored, and `\colorbox`, `\boxed`, `\fbox`, fraction bars, `\sqrt` and `\overline` are unchanged. **The final check:** - **iPhone:** whole pairs at 3x, and the rule in the text colour in light and dark. - **#314's baseline:** unchanged. - **Android:** pixel-identical to main except the rule colour. - **Mechanical:** 158 tests, and unchanged fingerprints (OTA). It reported BROKEN only on three points: - the lift is 3.26 pt and a lone-pair card grows by 6 pt, which is the round 2 rounding margin and was expected, though the check's brief still said 2.76 pt and 4 to 5 pt; - an author's explicit `mathbackground="black"` on a raw `mspace` is recoloured, a limitation accepted in review because it can't be told from MathJax's default. Following the owner's standing instruction to act without asking, these are accepted and the PR is merged. Closes #315. Closes #317. Verified: checks, /code-review, codex, /prove-learnharness (all behaviour confirmed; BROKEN only on an outdated brief and an accepted limitation)
On the phone, stretched math drew in loose pieces: `\underbrace`, `\overbrace`, `\xrightarrow`, `\overrightarrow`, `\overline`, tall `\left\{`. MathJax places the stretched middles as nested `<svg>` elements with `x`/`y`, and react-native-svg 15 drops a nested `<svg>`'s `x`/`y` on both platforms (`RNSVGGroup.mm`, `SvgView.drawChildren`). The pieces landed at their group's origin: the double bars under the owner's floor bracket and the ":" beside the "1".
**The fix:**
- `drawTex` (`apps/mobile/src/choice-math.ts`) wraps every nested `<svg>`, at any depth, in its own `<g>`, carrying its offset as `translate(X,Y)`. That also contains any clip or viewBox state the library leaves behind.
- Offsets are parsed with the flattener's shared `SVG_NUMBER` grammar, and anything else falls back to the source text.
- The #279 safety allowlist still checks the final markup.
**Checks:**
- **Pixel tests:** a resvg pixel test with `@resvg/resvg-js` as a dev dependency covers 29 expressions, in both themes, inline and display, and matches MathJax's intended rendering. Unaffected math is byte-identical.
- **The iPhone fixture:** it now includes the owner's equation and an `\xrightarrow`.
- **The final check:** it showed both underbraces and the arrow whole, matching the web in light and dark. #314's baseline was pixel-identical to main.
- **Tests and shipping:** 165 mobile tests passed, and the fingerprints are unchanged (OTA).
Android wasn't driven, because its host was restarting.
Closes #338.
Verified: checks, /code-review, codex, /prove-learnharness
What changed and why
Owner bug 1: on iPhone, the math in the answered quiz card ("4", "8", the signed pair) sits below the text around it. #313 is the same bug seen from the period's side: the math sits low, so a period after it looks raised. Fixes #313.
Measured. In the iPhone check's shots from before any lift (run 44871251269, 3x), a digit's ink bottom sits 12 to 13 px (4.0 to 4.3 pt) below the bottom of the letters on 16 pt text with 24 pt lines. That covers the answered card, the explanation, the message prose and the table rows. On a 14 pt choice row ("Train: 4; test: 8", 21 pt line) it sits 9 px (3.0 pt) low. After the fix (run 44884978315) the same spots read 0 to -1 px; the table is in the PR comments.
Cause. React Native 0.88.1's iOS text sets an inline view's frame at
glyphRect.origin.y + glyphRect.size.height - attachmentSize.height + font.descender(RCTTextLayoutManager.mm:631). That puts the view's bottom at the line's bottom plus the descender of its character's font. Two things make that the wrong place:RCTNSAttributedStringFragmentFromFragment,RCTAttributedTextUtils.mm:409-419), so the font it reads is TextKit's default, Helvetica 12. Its descender is 2.76 pt at any text size, where the text's is 0.4 em (6.4 pt at 16 pt).(lineHeight - tallest font line height) / 2over the whole Text (RCTApplyBaselineOffsetForRange,RCTAttributedTextUtils.mm:345-384). The frame ignores that.So the view sits
line descent + baselineOffset - 2.76below the baseline: 6.4 + 0.61 - 2.76 = 4.25 pt at 16/24 (measured 4.17 on average). Upstream fixed this by placing the view from the baseline TextKit reports for its line (react/react-native#59921, 5c062e6dd3, in no release yet). Its message names both faults.Fix, JS only, in
apps/mobile/src/MathLabel.tsx:inlineLiftcomputes that distance on iOS, in points at the font scale (Helvetica's descender doesn't grow with Dynamic Type), and the inlineMathPictureis moved up by it with atranslateY.inlineLift).NativeMarkdown.tsxgives each equation its line's fonts, which fixes the period after message math.Tests,
apps/mobile/tests/math-baseline.test.ts:\rulemath stays inside its line.Verified
math-baseline.test.ts: 7 tests). The round 2 tests fail on 6a27bf4, and mutations (no transform, no cap, lifting math-only lines) are each caught.npm run typecheckandnpm run lintinapps/mobile: pass.npx expo-updates runtimeversion:resolve --platform ios) isb294c68b58b762e9cac472a7ee00c0a6127b4216, the same as main, so this ships as an OTA update.iphone-screenshots, measured at the same spots, plus the fixture's tall-rule paragraphs.Source
node_modules/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm:621-632(attachment frames)RCTAttributedTextUtils.mm:345-402(RCTApplyBaselineOffsetForRange, applied over the whole Text) and:405-419(attachment fragments have no font)